package com.example.administrator.newproject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.newproject.R;
import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;

public class QRCodeScanActivity extends ActionBarActivity {

    private Button button;
    private Button button2;
    private Bitmap bitmap;
    private EditText edit;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode_scan);
        Drawable drawable
                =getResources().getDrawable(R.drawable.btn_red_round);

        getSupportActionBar().setBackgroundDrawable(drawable);
        getSupportActionBar().setTitle("二维码扫描");


        initView();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent();
                i.setClass(QRCodeScanActivity.this, CaptureActivity.class);

                startActivityForResult(i,0);
            }
        });


        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text=edit.getText().toString();

                if (TextUtils.isEmpty(text)){

                    Toast.makeText(QRCodeScanActivity.this, "请输入内容!", Toast.LENGTH_SHORT).show();


                }else {

                    try {
                        bitmap = EncodingHandler.createQRCode(text, 500);
                        imageView.setImageBitmap(bitmap);
                    } catch (WriterException e) {
                        e.printStackTrace();
                    }


                }

            }
        });




    }

    public void initView(){

        button = (Button) findViewById(R.id.qr_button1);
        button2 = (Button) findViewById(R.id.qr_button2);

        edit = (EditText) findViewById(R.id.qr_eidt);
        imageView = (ImageView) findViewById(R.id.qr_imageView);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==RESULT_OK){

            final String url=data.getExtras().getString("result");

            new AlertDialog.Builder(this)
                    .setTitle("扫描结果")
                    .setMessage(url)
                    .setNegativeButton("取消",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            onKeyDown(KeyEvent.KEYCODE_BACK,null);

                        }
                    })
                    .setPositiveButton("连接",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent=new Intent();
                            intent.setClass(QRCodeScanActivity.this,WebViewActivity.class);
                            intent.putExtra("url",url);
                            startActivity(intent);

                        }
                    }).show();

            //Toast.makeText(QRCodeScanActivity.this,url,Toast.LENGTH_SHORT).show();

        }
    }

}
